package org.example.domain.dto;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.example.domain.vo.home.Histogram;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.List;
import java.util.function.Function;

@Data
@ApiModel(description = "")
public class DTO<V> {
    @ApiModelProperty("今日销售金额")
    private String todaySaleMoney;
    
    @ApiModelProperty("今日零售金额")
    private String todayRetailMoney;
    
    @ApiModelProperty("今日采购金额")
    private String todayPurchaseMoney;
    
    @ApiModelProperty("昨日销售金额")
    private String yesterdaySaleMoney;
    
    @ApiModelProperty("昨日零售金额")
    private String yesterdayRetailMoney;
    
    @ApiModelProperty("昨日采购金额")
    private String yesterdayPurchaseMoney;
    
    @ApiModelProperty("本月累计销售")
    private String monthSaleMoney;
    
    @ApiModelProperty("本月累计零售")
    private String monthRetailMoney;
    
    @ApiModelProperty("本月累计采购")
    private String monthPurchaseMoney;
    
    @ApiModelProperty("今年累计销售")
    private String yearSaleMoney;
    
    @ApiModelProperty("今年累计零售")
    private String yearRetailMoney;
    
    @ApiModelProperty("今年累计采购")
    private String yearPurchaseMoney;
    
    
    
    @ApiModelProperty("授权用户数量")
    private Integer grantUserNumber;
    
    @ApiModelProperty("销售统计")
    private List<Histogram> saleList;
    
    @ApiModelProperty("零售统计")
    private List<Histogram> retailList;
    
    @ApiModelProperty("采购统计")
    private List<Histogram> purchaseList;
    
    public static <PO, VO> DTO<VO> of(PO p, Function<PO, VO> convertor) {
        VO vo = convertor.apply(p);
        DTO<VO> dto = BeanUtil.copyProperties(vo, DTO.class);
        return dto;
    }
    
    public static void shieldPrice(Object obj, List<String> names){
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                names.forEach(name -> {
                    if (field.getName().contains(name)) {
                        try {
                            if (field.getType() == BigDecimal.class) {
                                field.set(obj, new BigDecimal("0"));
                            }else {
                                field.set(obj, "***");
                            }
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    public static void removeTrailingZeros(Object obj){
        try {
            // 获取所有对象
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                if (field.getType() == String.class){
                    field.setAccessible(true);
                    String value = (String) field.get(obj);
                    if (value != null){
                        String s = removeTrailingZeros(value);
                        field.set(obj, s);
                    }
                }
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    
    private static String removeTrailingZeros(String str) {
        if (str.contains(".")) {
            str = str.replaceAll("0*$", "");
            str = str.replaceAll("\\.$", "");
        }
        return str;
    }
}
